﻿package mortal.game.scene3D.ai.ais
{
    import com.gengine.global.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.event.*;
    import mortal.component.gconst.*;
    import mortal.game.control.subControl.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.scene3D.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.base.*;
    import mortal.game.scene3D.ai.data.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;

    public class MoveByPathAI extends AICommand
    {
        protected var _paths:Array;
        protected var _scene:GameScene3D;
        protected var _targetPixPoint:Point;
        protected var _meRole:MovePlayer;
        protected var _range:int = 0;
        protected var _isStoped:Boolean = true;
        private var _samePlaceTimes:int;
        private var _lastPlace:int;
        private var _breakTimes:int = 0;
        public static var working:Boolean = false;
        public static var curDis:int = -1;

        public function MoveByPathAI()
        {
            return;
        }// end function

        override public function start(param1:Function = null) : void
        {
            working = true;
            this._scene = _data.scene;
            this._meRole = _data.meRole;
            this._paths = _data.params as Array;
            _callback = param1;
            this._isStoped = false;
            this.updateTilePoint();
            this._breakTimes = 0;
            if (this.isInRange())
            {
                this.stop();
                return;
            }
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.ENTITY_DEAD, this.onEntityDead, false, 0, true);
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.GIRD_WALK_END, this.onGirdWalkEnd, false, 0, true);
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.WALK_END, this.onWalkEnd, false, 0, true);
            FrEventDispatcher.instance.proxyAddEventListener(this._meRole, PlayerEvent.WALK_BREAK, this.onWalkBreak, false, 0, true);
            Dispatcher.dispatchEvent(new DataEvent(EventName.AI_MoveStart, this._meRole.pathArray));
            GuideEffect.showAutoPathStatus();
            var _loc_2:* = GameMapUtil.getDistance(this._meRole.x2d, this._meRole.y2d, this._targetPixPoint.x, this._targetPixPoint.y) - this.range;
            if (_loc_2 >= GameConst.UpMountMinDistance)
            {
                GameController.guide.checkAndUpMount();
            }
            this.sceneMove();
            this.disposeTimer();
            this.startTimerChecker();
            return;
        }// end function

        protected function startTimerChecker() : void
        {
            this._samePlaceTimes = 0;
            this._lastPlace = -1;
            Global.instance.addEnterSecond(this.timeToCheck);
            return;
        }// end function

        protected function timeToCheck() : void
        {
            var _loc_1:* = AIManager.curAI;
            if (!AIManager.isWorking || _loc_1 is AutoFightAI && AutoFightAI.instance.curAI != this || _loc_1 is FollowFightBossToDeadAI && (_loc_1 as FollowFightBossToDeadAI).curAI != this || !(_loc_1 is AutoFightAI) && !(_loc_1 is FollowFightBossToDeadAI) && _loc_1 != this)
            {
                this.callback = null;
                this.stop();
                return;
            }
            var _loc_2:* = RolePlayer.instance.x2d * 90000 + RolePlayer.instance.y2d;
            if (_loc_2 == this._lastPlace)
            {
                var _loc_3:String = this;
                var _loc_4:* = this._samePlaceTimes + 1;
                _loc_3._samePlaceTimes = _loc_4;
                if (this._samePlaceTimes >= 3)
                {
                    this.onWalkBreak(null);
                }
            }
            else
            {
                this._samePlaceTimes = 0;
            }
            this._lastPlace = _loc_2;
            return;
        }// end function

        protected function disposeTimer() : void
        {
            Global.instance.removeEnterSecond(this.timeToCheck);
            return;
        }// end function

        override public function set data(param1:AIData) : void
        {
            super.data = param1;
            if (param1.range > 0)
            {
                this._range = param1.range;
            }
            return;
        }// end function

        override public function stop(param1:Boolean = true) : void
        {
            this._breakTimes = 0;
            this.disposeTimer();
            if (this._meRole)
            {
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.ENTITY_DEAD, this.onEntityDead);
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.GIRD_WALK_END, this.onGirdWalkEnd);
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.WALK_END, this.onWalkEnd);
                FrEventDispatcher.instance.proxyRemoveEventListener(this._meRole, PlayerEvent.WALK_BREAK, this.onWalkBreak);
                if (this._meRole.isMove)
                {
                    this._meRole.stopWalking(param1);
                }
            }
            this._isStoped = true;
            this._range = 0;
            super.endAI();
            Dispatcher.dispatchEvent(new DataEvent(EventName.AI_MoveEnd));
            GuideEffect.showAutoPathStatus(false);
            working = false;
            curDis = -1;
            return;
        }// end function

        protected function sceneMove() : void
        {
            if (this._paths == null || this._paths.length == 0)
            {
                return;
            }
            this._scene.moveRoleByPath(this._paths);
            return;
        }// end function

        protected function onEntityDead(event:Event) : void
        {
            this.stop();
            return;
        }// end function

        protected function onWalkEnd(event:Event) : void
        {
            if (this.isInRange())
            {
                this.stop();
            }
            return;
        }// end function

        protected function onWalkBreak(event:Event) : void
        {
            if (RolePlayer.instance.entityInfo == null || RolePlayer.instance.entityInfo.isDead || !BuffUtil.isCanRoleWalk())
            {
                this.stop();
                return;
            }
            var _loc_2:String = this;
            var _loc_3:* = this._breakTimes + 1;
            _loc_2._breakTimes = _loc_3;
            if (this.isInRange() || this._breakTimes >= 15)
            {
                this.stop();
                return;
            }
            this.updateTilePoint();
            this.updatePath();
            this.sceneMove();
            return;
        }// end function

        public function updatePath() : void
        {
            var _loc_1:* = Scene3DClickProcessor.findPath(this._meRole.x2d, this._meRole.y2d, this._targetPixPoint.x, this._targetPixPoint.y, true);
            if (_loc_1.length == 0)
            {
                this.stop();
                return;
            }
            this._paths = _loc_1[0] as Array;
            return;
        }// end function

        protected function onGirdWalkEnd(event:Event) : void
        {
            if (this.isInRange())
            {
                this.stop();
            }
            return;
        }// end function

        protected function isInRange() : Boolean
        {
            try
            {
                curDis = GameMapUtil.getDistance(this._meRole.x2d, this._meRole.y2d, this._targetPixPoint.x, this._targetPixPoint.y);
            }
            catch (e)
            {
            }
            var _loc_1:* = curDis <= this.range;
            return _loc_1;
        }// end function

        public function get range() : Number
        {
            if (this._range >= 0)
            {
                return this._range;
            }
            return 0;
        }// end function

        public function set range(param1:Number) : void
        {
            this._range = param1;
            return;
        }// end function

        protected function updateTilePoint() : void
        {
            this._targetPixPoint = new Point();
            if (this._paths == null || this._paths.length == 0)
            {
                return;
            }
            var _loc_1:* = this._paths[(this._paths.length - 1)];
            this._targetPixPoint.x = _loc_1._x;
            this._targetPixPoint.y = _loc_1._y;
            return;
        }// end function

    }
}
